use crate::utils::logger::{add_file_msg,update_react_project};
use anyhow::Result;
use rust_embed::RustEmbed;

#[derive(RustEmbed)]
#[folder = "template-state-redux"]
struct Asset;

pub fn copy_redux_template_to_project(
    project_dir: &std::path::PathBuf,
) -> Result<(), std::io::Error> {
    update_react_project("redux样板的配置");

    // 创建指向 project_dir/src 的路径
    let react_src = project_dir.join("src");
    // std::fs::create_dir_all(&hook_path)?;
    // 将template-customize-hook的内容复制到src/hook下
    for filename in Asset::iter() {
        let file_contents = Asset::get(filename.as_ref()).unwrap();
        let mut file_path = std::path::PathBuf::from(&react_src);
        file_path.push(filename.as_ref());
        let mut directory_path = std::path::PathBuf::from(&file_path);
        directory_path.pop();

        add_file_msg(filename.as_ref());
        std::fs::create_dir_all(directory_path)?;
        std::fs::write(file_path, file_contents.data)?;
    }

    Ok(())
}